package util;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


//StringBuffer is synchronized, StringBuilder is not.
//http://stackoverflow.com/questions/355089/stringbuilder-and-stringbuffer-in-java

/**
 *
 * @author Rdelacruz
 * @version 2014.12.18
 */
public class StringUtil {
    
    //http://stackoverflow.com/questions/7488643/java-how-to-convert-comma-separated-string-to-arraylist
    public static String [] splitPorComasEspaciosToArray(String cadena) {
        return cadena.split("\\s*,\\s*");//considera 0 o mas espacios antes y despues de la coma
    }
    
    public static String [] splitPorComasToArray(String cadena) {
        return cadena.split(","); //no toma en cuenta los espacios antes y despues de las comas
    }
    
    public static String [] splitToArray(String cadena, String separadorEnExpresionRegular) {
        return cadena.split(separadorEnExpresionRegular);
    }
    
    //http://stackoverflow.com/questions/10631715/splitting-a-comma-separated-string
    public static List<String> splitToList(String cadena, String separadorEnExpresionRegular) {
        return Arrays.asList( cadena.split(separadorEnExpresionRegular) );
    }
    
    //http://stackoverflow.com/questions/205555/the-most-sophisticated-way-for-creating-comma-separated-strings-from-a-collectio
    //http://stackoverflow.com/questions/6622974/convert-string-to-comma-separated-string-in-java
    public static String unirSeparadoPor(String cadenas[], String separador) {
        StringBuilder sb = new StringBuilder();
        for(String string : cadenas) {
            sb.append(string);
            //sb.append(",");//para el caso de una coma
            sb.append(separador);
        }
        //return sb.length() > 0 ? sb.substring(0, sb.length() - 1): "";//para el caso de una coma
        return sb.length() > 0 ? sb.substring(0, sb.length() - separador.length()): "";
    }
    
    public static String unirSeparadoPor(List<String> cadenas, String separador) {
        StringBuilder sb = new StringBuilder();
        for(String string : cadenas) {
            sb.append(string);
            sb.append(separador);
        }
        return sb.length() > 0 ? sb.substring(0, sb.length() - separador.length()): "";
    }
    
    public static String repetirTexto(String texto, int nRepeticiones) {
        String rep = "";
        for(int i=0; i<nRepeticiones; i++) {
            rep = rep.concat(texto);
        }
        return rep;
    }
    public static Integer StringtoInteger(String numero) {
        if(numero!=null) {
            numero = numero.trim();
            if(!numero.isEmpty())
                return Integer.valueOf(numero);
        }
        return null;
        //return numero!=null ? Integer.valueOf(numero.trim()) : null;        
    }
    public static int StringtoInt(String numero) {
        if(numero!=null) {
            numero = numero.trim();
            if(!numero.isEmpty())
                return Integer.parseInt(numero);
        }
        return 0;
        //return numero!=null ? Integer.parseInt(numero.trim()) : 0;
    }
    
    public static Boolean StringtoBoolean(String valor) {
        return StringtoBoolean(valor,"0");
    }
    
    public static Boolean StringtoBoolean(String valor, String valorFalso) {
        if(valorFalso==null)//para valores de checkbox
            return valor!=null;
        
        return valor!=null ? ! valor.trim().equals(valorFalso) : null;
    }
    
    public static String trim(String texto) {
        return texto==null? null : texto.trim();
    }
    
    //http://stackoverflow.com/questions/782178/how-do-i-convert-a-string-to-an-inputstream-in-java
    public static InputStream stringToInputStream(String texto) {
        return new ByteArrayInputStream(texto.getBytes());
    }
            
            
    public static byte[] stringToBytes(String texto, boolean textoEsUTF8) 
            throws UnsupportedEncodingException {
        
        //http://stackoverflow.com/questions/20227/how-do-i-use-3des-encryption-decryption-in-java
        if(textoEsUTF8) {
            return texto.getBytes("utf-8");
            //return texto.getBytes(Charset.forName("UTF-8")); 
        }
            
        //http://stackoverflow.com/questions/18571223/convert-java-string-to-byte-array
        return texto.getBytes();
    }

    public static BigDecimal StringtoBigDecimal(String valor) {
        String v = trim(valor);
        if(v!=null && !v.isEmpty())
            return new BigDecimal(v);
        return null;
    }

    public static boolean esVacioONulo(String valor) {
        return valor==null || valor.trim().isEmpty();
    }
    
    public static String filtrarVacioONulo(String valor) {
        return esVacioONulo(valor) ? null : valor;
    }
    
    public static List<Integer> indicesDe(String texto, char caracter) {
    	List<Integer> indices = new ArrayList<Integer>();
    	for(int i =0; i < texto.length(); i++)
    	    if(texto.charAt(i) == caracter)
    	        indices.add(i);
    	return indices;
    }
    
    
    public static String replaceParameters(
    		String texto,
    		Map<Integer,String> parametros) {
    	return replaceParameter(texto,parametros,"\\?");//usa el patron '?' por defecto
    }
  
    //Autor: Ramón
    public static String replaceParameter(
    		String texto,    		
    		Map<Integer,String> parametros,
    		String patron) {
		String[] partes = texto.split(patron, -1);   		
		if(partes.length>1) {//si existen caracteres del patron
			StringBuilder sb =  new StringBuilder();
			sb.append(partes[0]);
			for(int i=1; i<partes.length; i++) {
				sb.append(parametros.get(i));
				sb.append(partes[i]);
			}
			return sb.toString();
		}
		return texto;
    }
    
}
